package com.example.emos.wx;

import cn.hutool.core.util.StrUtil;
import com.example.emos.wx.core.config.SystemConstants;
import com.example.emos.wx.checkin.db.dao.SysConfigDao;
import com.example.emos.wx.checkin.model.pojo.SysConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;


// Generated by https://start.springboot.io
// 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn

@SpringBootApplication
@ServletComponentScan
@Slf4j
@EnableAsync
public class EmosWxApiApplication  {
    @Autowired
    private SysConfigDao sysConfigDao;
    @Value("${emos.image-folder}")
    private String imageFolder;

    @Autowired
    private SystemConstants systemConstants;
    public static void main(String[] args) {
        SpringApplication.run(EmosWxApiApplication.class, args);
    }

    @PostConstruct
    public void init(){
//        从数据库中获取常量配置信息
       List<SysConfig> list = sysConfigDao.selectAllParam();
//       遍历每一项常量配置信息
       list.forEach(one->{
//           从每一项常量配置中获得param_key
           String key = one.getParamKey();
//           使用驼峰命名存储数据
           key = StrUtil.toCamelCase(key);
//           从每一项常量配置中获得param_value
           String value = one.getParamValue();
//           创建图片目录,如果不存在就直接创建否则创建
           new File(imageFolder).mkdirs();
           try{
//               使用反射为systemConstants赋值
               Field field = systemConstants.getClass().getDeclaredField(key);
               field.set(systemConstants,value);
           }catch (Exception e){
               log.error("执行异常",e);
           }
       });
    }

}
